91A - Newspaper Headline - CodeForces Solution


greedy strings *1500

Please click on ads to support us..

Python Code:

import copy

if __name__ == "__main__":
    s, t = str(input()), str(input())
    n = len(s)
    pos = [0] * 26
    for i in range(26):
        pos[i] = n
    nxt = [[0] * 26] * n
    for k in range(2):
        for i in range(n-1, -1, -1):
            nxt[i] = copy.deepcopy(pos)
            pos[ord(s[i]) - ord("a")] = i

    ans, i = 1, 0
    for j in range(1 if s[0] == t[0] else 0, len(t), 1):
        next = nxt[i][ord(t[j]) - ord("a")]
        if next == n:
            print(-1)
            exit()
        if next <= i:
            ans += 1
        i = next
    print(ans)

C++ Code:

#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> p32;
typedef pair<ll, ll> p64;
typedef pair<double, double> pdd;
typedef vector<ll> v64;
typedef vector<int> v32;
typedef vector<vector<int> > vv32;
typedef vector<vector<ll> > vv64;
typedef vector<vector<p64> > vvp64;
typedef vector<p64> vp64;
typedef vector<p32> vp32;
ll MOD = 1000000007;
double eps = 1e-12;
#define ln "\n"
#define printVector(a) for(int i=0; i<a.size(); i++){cout<<a[i]<<" ";}cout<<ln;
#define print_array(a,n) for(int i=0; i<n; i++){cout<<a[i]<<" ";}cout<<ln;
#define dbg(x) cout<<#x<<" = "<<x<<ln
#define mp make_pair
#define pb push_back
#define take_vector(a) for(auto &x:a)cin>>x;
#define take_array(a,n) for(int i=0;i<n;i++){cin>>a[i];}
#define take_matrix(a,m,n) for(int i=0; i<m; i++){for(int j=0; j<n; j++){cin>>a[i][j];}}
#define print_matrix(a,m,n) for(int i=0; i<m; i++){for(int j=0; j<n; j++){cout<<a[i][j]<<" ";}cout<<ln;}
#define fi first
#define se second
#define INF 2e18
#define fast_cin() ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL)
#define all(x) (x).begin(), (x).end()
#define sz(x) ((ll)(x).size())
#define gcd(a,b) __gcd(a,b)
#define lcm(a,b) (a/gcd(a,b))*b
#define count(a,x) count(a.begin(), a.end(),x)
#define sum(a) accumulate(a.begin(), a.end(),0)
#define max_ele(a) *max_element(a.begin(), a.end())
#define min_ele(a) *min_element(a.begin(), a.end())
void solve() {
	string s, t;
	cin >> s >> t;
	int n = s.size();
	int m = t.size();
	set<int> pos[26];
	for(int i = 0; i<n; i++) {
		pos[s[i]-'a'].insert(i);
	}
	int ans = 1, lastIndex = -1;
	for(int i= 0; i<m; i++) {
		int ch = t[i]-'a';
		if(pos[ch].size() ==  0) {
			cout << -1 << ln;
			return; 
		}
		auto it = pos[ch].upper_bound(lastIndex);
		if(it == pos[ch].end()) {
			ans++;
			lastIndex = *pos[ch].begin();
		}
		else{
			lastIndex = *it;
		}
	}
	cout << ans << ln;
}
int main()
{
#ifndef ONLINE_JUDGE
  // freopen("input.txt","r",stdin);
  // freopen("output.txt","w",stdout);
#endif
  fast_cin();
  ll t = 1;
  // cin >> t;
  // for (int it = 1; it <= t; it++)
  // {
  //   //cout << "Case #" << it+1 << ": ";
  //   solve();
  // }
  solve();
  return 0;
}


Comments

Submit
0 Comments
More Questions

589. N-ary Tree Preorder Traversal
1299. Replace Elements with Greatest Element on Right Side
1768. Merge Strings Alternately
561. Array Partition I
1374. Generate a String With Characters That Have Odd Counts
1822. Sign of the Product of an Array
1464. Maximum Product of Two Elements in an Array
1323. Maximum 69 Number
832. Flipping an Image
1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies